<?php 
/*
 module:		MBTI题目选项
 create_time:	2024-04-18 16:37:17
 author:		
 contact:		
*/

namespace app\admin\controller\Mbti;

use app\admin\service\Mbti\OptionService;
use app\admin\model\Mbti\Option as OptionModel;
use app\admin\controller\Admin;
use think\facade\Db;

class Option extends Admin {


 /**start**/
	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $offset = $this->request->post('offset', 0, 'intval');
            $page   = floor($offset / $limit) +1 ;

            $where = [];
            $where['subject_id'] = $this->request->param('subject_id', '', 'serach_in');

            $order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

            $field = 'mo_id,option_title,factor,fraction,fraction2';
            $orderby = ($sort && $order) ? $sort.' '.$order : 'mo_id desc';

            $res = OptionService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
            return json($res);
		}
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
            $mbti_id= $this->request->get("mbti_id");
            $list= Db::name('mbti_factor')->where(["mbti_id"=>$mbti_id])->field("mf_id,factor_name,factor_iden")->select()->toArray();
            $this->view->assign("list",$list);
			return view('add');
		}else{
			$postField = 'option_title,factor,fraction2,fraction,subject_id';
			$data = $this->request->only(explode(',',$postField),'post',null);
            Db::name("mbti_subject")->where(["subject_id"=>$data['subject_id']])->inc("option_num")->update();
			$res = OptionService::add($data);
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function edit(){
        if (!$this->request->isPost()){
            $mo_id = $this->request->get('mo_id','','serach_in');
            if(!$mo_id) $this->error('参数错误');
            $mbti_id= $this->request->get("mbti_id");
            $list= Db::name('mbti_factor')->where(["mbti_id"=>$mbti_id])->field("mf_id,factor_name,factor_iden")->select()->toArray();
            $this->view->assign("list",$list);

            $this->view->assign('info',checkData(OptionModel::find($mo_id)));
            return view('edit');
        }else{
            $postField = 'mo_id,option_title,factor,fraction,subject_id';
            $data = $this->request->only(explode(',',$postField),'post',null);
            $res = OptionService::edit($data);
            return json(['status'=>'00','msg'=>'修改成功']);
        }
	}

	/*删除*/
	function del(){
		$idx =  $this->request->post('mo_id', '', 'serach_in');
        $subject_id = $this->request->get("subject_id");
		if(!$idx) $this->error('参数错误');
		try{
			OptionModel::destroy(['mo_id'=>explode(',',$idx)],true);
            $count = count(explode(',',$idx));
            Db::name("mbti_subject")->where(["subject_id"=>$subject_id])->dec("option_num",$count)->update();
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}




    /**end**/



}

